package br.com.dev.tcc.model;

import static br.com.dev.tcc.builder.PhoneIntentBuilder.buildIntent;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Location;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.BaseItem;
import br.com.dev.tcc.builder.BaseViewItemBuilder;
import br.com.dev.tcc.singleton.LocationSingleton;
import br.com.dev.tcc.view.LocaisActivity;
import br.com.dev.tcc.view.apolice.ApolicesActivity;
import br.com.dev.tcc.view.apolice.PreferenciasActivity;
import br.com.dev.tcc.view.corretor.ListaCorretoresActivity;
import br.com.dev.tcc.view.sinistro.SinistroActivity;
import br.com.dev.tcc.view.utilitarios.UtilitariosActivity;

/**
 * Constroi os itens do menu de chamadas telefonicas e os itens da grade da tela
 * inicial
 */
public class HomeModel {

	private Context ctx;
	private List<BaseItem> gridItens;
	private List<BaseItem> phoneItens;

	private BaseViewItemBuilder builder;

	public HomeModel(Context ctx) {

		this.ctx = ctx;
		gridItens = new ArrayList<BaseItem>();
		phoneItens = new ArrayList<BaseItem>();

		builder = new BaseViewItemBuilder(ctx);
		buildItensTelefone();
		buildGridItens();
	}

	/**
	 * Constroi os itens disponiveis para chamadas telefonicas
	 */
	private void buildItensTelefone() {

		buildItemSeguradora();
		buildItemSamu();
		buildItemBombeiros();
		buildItemPolicia();
		buildItemPoliciaRodoviaria();
	}

	private void buildItemSamu() {

		phoneItens.add(builder.build(R.string.samu, buildIntent(ctx
				.getResources().getString(R.string.telefone_samu))));
	}

	private void buildItemBombeiros() {

		phoneItens.add(builder.build(R.string.bombeiros, buildIntent(ctx
				.getResources().getString(R.string.telefone_bombeiros))));
	}

	private void buildItemPolicia() {

		phoneItens.add(builder.build(R.string.policia, buildIntent(ctx
				.getResources().getString(R.string.telefone_policia))));
	}

	private void buildItemPoliciaRodoviaria() {

		phoneItens.add(builder.build(R.string.policia_rod, buildIntent(ctx
				.getResources().getString(R.string.telefone_policia_rod))));
	}

	private void buildItemSeguradora() {

		phoneItens.add(builder.build(R.string.seguradora, buildIntent(ctx
				.getResources().getString(R.string.telefone_seguradora))));
	}

	private void buildGridItens() {

		buildItemPerfil();
		buildItemSinistro();
		buildItemLocais();
		buildItemUtilitarios();
		buildItemCorretor();
		buildItemPreferencias();
	}

	private void buildItemPerfil() {

		gridItens.add(builder.build(R.drawable.perfil, R.string.perfil,
				ApolicesActivity.class));
	}

	private void buildItemSinistro() {

		gridItens.add(builder.build(R.drawable.sinistro, R.string.sinistro,
				SinistroActivity.class));
	}

	private void buildItemLocais() {

		gridItens.add(builder.build(R.drawable.locais, R.string.locais,
				LocaisActivity.class));
	}

	private void buildItemUtilitarios() {

		gridItens.add(builder.build(R.drawable.utilitarios,
				R.string.utilitarios, UtilitariosActivity.class));
	}

	private void buildItemCorretor() {

		gridItens.add(builder.build(R.drawable.corretor, R.string.corretor,
				ListaCorretoresActivity.class));
	}

	private void buildItemPreferencias() {

		gridItens.add(builder.build(R.drawable.preferencias_sino,
				R.string.preferencias, PreferenciasActivity.class));
	}

	/**
	 * @return Array com os itens da grade
	 */
	public List<BaseItem> getGridItens() {

		return gridItens;
	}

	/**
	 * @return Array com os itens para chamadas telefonicas
	 */
	public List<BaseItem> getTelefones() {

		return phoneItens;
	}

	/**
	 * @param isNetworkConnected
	 * @return Se baseia nas configurações de gps e internet para informar ao
	 *         usuário a necessidade de ativação
	 */
	public String getAlertText(boolean isGpsEnabled,
			boolean isNetLocationEnabled, boolean isNetworkConnected) {

		if (isGpsEnabled || (isNetLocationEnabled && isNetworkConnected)) {

			Location l = LocationSingleton.getInstance().getLocation();
			if (l != null)
				return ctx.getResources().getString(R.string.precisao)
						.replace("?", Float.toString(l.getAccuracy()));
			else
				return ctx.getResources().getString(R.string.obtendo_local);
		} else if (isNetworkConnected)
			return ctx.getResources().getString(R.string.sem_gps);
		else
			return ctx.getResources().getString(R.string.sem_conexao);
	}
}
